/*{
author=pk2.2（风铃等）
version=2.0
date=2022/12/03
}*/


/***
可以把军队解散,解散的军队会变成为1,5倍人口
***/

 // ## 2023/05/03 # 铃 # 修复bug,美化显示。 ##
 // ## 2023/05/02 # 铃 # 适配新的内政系统。 ##
 // ## 2023/05/01 # 铃 # 完善了新的参数。 ##
// ## 2022/12/03 # 铃 # 新做了解甲归田功能。 ##



namespace 解甲归田
{

	// ================ CUSTOMIZE ================
	// 全局常量 --------------------------------------------------
	/*[text=消耗技巧点 default=5 min=5 max=300]*/
	const int TP_COST = 50;

	/*[text=消耗行动力 default=50 min=50 max=300]*/
	const int ACTION_COST = 50;

	// ===========================================

	const int KEY = pk::hash("解甲归田");

	class Main
	{

		pk::building @building_;
		pk::force @force_;
		pk::person @taishu_;
		pk::person @farmer_;
		pk::person @merchant_;
		pk::person @officer_;
		pk::person @civilian_;
		pk::city @city_;

		Main()
		{
			pk::menu_item item;
			item.menu = 100;
			item.pos = 9;
			item.shortcut = "C";
			item.init = pk::building_menu_item_init_t(init);
			item.is_visible = pk::menu_item_is_visible_t(isVisible);
			item.is_enabled = pk::menu_item_is_enabled_t(isEnabled);
			item.get_text = pk::menu_item_get_text_t(getText);
			item.get_desc = pk::menu_item_get_desc_t(getDesc);
			item.handler = pk::menu_item_handler_t(handler);
			pk::add_menu_item(item);
		}

		void init(pk::building @building)
		{
			@building_ = @building;
			@force_ = pk::get_force(building.get_force_id());
			@taishu_ = pk::get_person(pk::get_taishu_id(building));
			@farmer_ = pk::get_person(武将_农民);
			@officer_ = pk::get_person(무장_무관);
			@merchant_ = pk::get_person(武将_商人);
			@civilian_ = pk::get_person(무장_문관);
			@city_ = pk::building_to_city(building);
		}

		bool isVisible()
		{
			// pk::trace("taishu_.service" + taishu_.service + "building_.get_id()" + building_.get_id());
			if (taishu_ is null or taishu_.service != building_.get_id())
				return false;
			return true;
		}

		bool isEnabled()
		{
			if (taishu_ is null or pk::is_absent(taishu_) or pk::is_unitize(taishu_))
				return false;
			else if (taishu_.action_done == true)
				return false;
			else if (force_.tp < TP_COST)
				return false;
			else if (pk::get_district(pk::get_district_id(force_, 1)).ap < ACTION_COST)
				return false;
			return true;
		}

		string getText()
		{
			return pk::encode("解甲归田");
		}

		string getDesc()
		{
			if (pk::is_absent(taishu_) or pk::is_unitize(taishu_))
				return pk::encode("太守不在.");
			else if (taishu_.action_done == true)
				return pk::encode("太守已行动.");
			else if (force_.tp < TP_COST)
				return pk::encode(pk::format("技巧不足.(需要{}技巧)", TP_COST));
			else if (pk::get_district(pk::get_district_id(force_, 1)).ap < ACTION_COST)
				return pk::encode(pk::format("行动力不足(需要{}行动力)", ACTION_COST));
			else
				return pk::encode(pk::format("实施解甲归田.(需要 {}行动力)", ACTION_COST));
		}

		bool handler()
		{

			if (pk::choose(pk::encode("要实施解甲归田吗?"), {pk::encode(" 是 "), pk::encode(" 否 ")}) == 1)
				return false;
			pk::scene(pk::scene_t(scene_Event));
			auto district = pk::get_district(pk::get_district_id(force_, 1));
			pk::add_ap(district, -ACTION_COST);

			taishu_.action_done = true;

			return true;
		}

		void scene_Event()
		{

			int n = pk::rand(10);

			if (n < 10)
			{
				pk::fade(0);
				pk::sleep();
				pk::background(9);
				pk::fade(255);

				pk::message_box(pk::encode("土地荒芜，应该让士兵回乡种田."), taishu_);
				pk::message_box(pk::encode("连年征战，有许多土地已经无人耕种，大人此举意义深远."), officer_);
				pk::message_box(pk::encode("是的！如此方能立足一方."), taishu_);

				auto base_p = ch::get_base_p(building_.get_id());
				pk::int_bool deal0;
				int max = building_.troops;

				deal0 = pk::numberpad(pk::encode("士兵量"), 0, max, 0, pk::numberpad_t(numberpad_t));
				int 解甲量 = deal0.first;

				pk::message_box(pk::encode(pk::format("城市把\x1b[1x{}\x1b[0x士兵退伍还乡.", int(解甲量))));
				pk::message_box(pk::encode(pk::format("城市人口增加了\x1b[1x{}\x1b[0x.", int(解甲量 * 0.8))));
				pk::message_box(pk::encode(pk::format("城市兵役人口增加了\x1b[1x{}\x1b[0x.", int(解甲量 * 0.2))));


				pk::add_troops(building_, -解甲量, true);
				base_p.mil_pop_all += int(解甲量 *0.2);		// 兵役人口相应增加
				base_p.population += int(解甲量 * 0.8); // 人口相应增加

				pk::fade(0);
				pk::sleep();
				pk::background(-1);
				pk::fade(255);
			}

		} // scene_Event()

		string numberpad_t(int line, int original_value, int current_value)
		{
			return pk::encode("");
		}
	}

	Main main;
}